<?php

declare(strict_types=1);

namespace app\validate\system;

use think\Validate;

class SysAccountValidate extends Validate
{
    protected $rule = [
        'id' => 'integer|gt:0',
        'phone' => 'regex:^1\d{10}$',
        'email' => 'email',
        'password' => 'min:6',
        'status' => 'in:0,1,2',
        'nickname' => 'max:50',
        'real_name' => 'max:50',
        'role_ids' => 'array',
        'role_ids.*' => 'integer|gt:0',
    ];

    protected $message = [
        'id.integer' => '用户ID格式不正确',
        'id.gt' => '用户ID格式不正确',
        'phone.regex' => '手机号格式不正确',
        'email.email' => '邮箱格式不正确',
        'password.min' => '密码长度至少 6 位',
        'status.in' => '状态值不正确',
        'nickname.max' => '昵称长度不能超过 50 个字符',
        'real_name.max' => '姓名长度不能超过 50 个字符',
        'role_ids.array' => '角色参数必须为数组',
        'role_ids.*.integer' => '角色ID必须为数字',
        'role_ids.*.gt' => '角色ID必须大于0',
    ];

    protected $scene = [
        'create' => [
            'phone',
            'email',
            'password',
            'status',
            'nickname',
            'real_name',
            'role_ids',
        ],
        'update' => [
            'id',
            'phone',
            'email',
            'password',
            'status',
            'nickname',
            'real_name',
            'role_ids',
        ],
    ];

    protected function sceneCreate(): self
    {
        return $this->only($this->scene['create'])
            ->append('phone', 'require|regex:^1\d{10}$')
            ->append('password', 'require|min:6');
    }

    protected function sceneUpdate(): self
    {
        return $this->only($this->scene['update'])
            ->append('id', 'require|integer|gt:0');
    }
}
